function formatTime(duration) {
    var hours = Math.floor(duration / 3600);
    var minutes = Math.floor((duration % 3600) / 60);
    var seconds = Math.floor(duration % 60);

    // 格式化小时部分
    var formattedHours = ('0' + hours).slice(-2);

    // 格式化分钟部分
    var formattedMinutes = ('0' + minutes).slice(-2);

    // 格式化秒数部分
    var formattedSeconds = ('0' + seconds).slice(-2);

    // 根据实际情况返回时间字符串
    if (hours > 0) {
        return formattedHours + ':' + formattedMinutes + ':' + formattedSeconds;
    } else if (minutes > 0) {
        return formattedMinutes + ':' + formattedSeconds;
    } else {
        return '00:' + formattedSeconds;
    }
}


function handleFilesAdded(files) {
    var file = files[0];
    if (file.type.startsWith('video') || file.type.startsWith('audio')) {
        var mediaElement = document.createElement(file.type.startsWith('video') ? 'video' : 'audio');
        mediaElement.src = URL.createObjectURL(file.getNative());
        mediaElement.addEventListener('loadedmetadata', function() {
            var duration = this.duration;
            var formattedDuration = formatTime(duration);
            console.log('文件时长：', formattedDuration);
            $('#hiddenField').remove();
            $('<input>').attr({
                type: 'hidden',
                id: 'hiddenField',
                value: formattedDuration
            }).appendTo('body');
            URL.revokeObjectURL(this.src);
        });
    }
}

var uploaderUp = new plupload.Uploader({
    browse_button: 'upFile',
    url: 'jjup/php/up.php',
    flash_swf_url: 'jjup/lib/plupload-2.1.2/js/Moxie.swf',
    silverlight_xap_url: 'jjup/lib/plupload-2.1.2/js/Moxie.xap',
    multi_selection: false,
    init: {
        PostInit: function () {
            document.getElementById('ossfile1_up').innerHTML = '';
        },
        FilesAdded: function (up, files) {
            handleFilesAdded(files);
            plupload.each(files, function (file) {
                document.getElementById('ossfile1_up').innerHTML += '<div class="progress-jd" id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ')<b></b>'
                    + '<div class="progress"><div class="progress-bar" style="width:0%"></div></div>'
                    + '</div>';
            });
            up.start();
        },
        BeforeUpload: function (up, file) {
            up.start();
        },
        UploadProgress: function (up, file) {
            var d = document.getElementById(file.id);
            d.getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
            var prog = d.getElementsByTagName('div')[0];
            var progBar = prog.getElementsByTagName('div')[0]
            progBar.style.width = 2 * file.percent + 'px';
            progBar.setAttribute('aria-valuenow', file.percent);
        },
        FileUploaded: function (up, file, info) {
            if (info.status == 200) {
                var jg = eval('(' + info.response + ')');
                if (jg.error == '0') {
                    $("#le_vod2").val(jg.url);
                    var duration = $('#hiddenField').val();
                    $('#shorttitle').val(duration);
                    $("#ossfile1_up").html('');
                    $("#console_up").html('');
                } else {
                    alert(jg.error);
                }
            }
            else {
                alert(info.response);
                //document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = info.response;
            }
        },
        Error: function (up, err) {
            if (err.code == -600) {
                document.getElementById('console_up').appendChild(document.createTextNode("\n选择的文件太大啦"));
            }
            else if (err.code == -601) {
                document.getElementById('console_up').appendChild(document.createTextNode("\n选择的文件后缀不对"));
            }
            else if (err.code == -602) {
                document.getElementById('console_up').appendChild(document.createTextNode("\n此文件已经上传过啦"));
            }
            else {
                document.getElementById('console_up').appendChild(document.createTextNode("\nError xml:" + err.response));
            }
        }
    }
});
uploaderUp.init();